<?php

App::uses('AppController', 'Controller');

class NivelesController extends AppController
{

    public $uses = array('Nivele', 'Paralelo', 'Materia', 'MateriasNivele', 'Gestione');
    public $layout = 'colegio';
    public $helper = array('Javascript', 'Ajax');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }

    public function index()
    {
        $gestion =date('Y');
        $niveles = $this->Nivele->find('all', array(
            'conditions'=>array('Nivele.gestion'=>$gestion),
            'recursive' => -1
                ));
        $this->set(compact('niveles'));
    }

    public function nivelesparalelos()
    {
        $niveles = $this->Nivele->find('all', array(
            'recursive' => 0
                ));
        $this->set(compact('niveles'));
    }
    
    public function ajaxparalelosnivel($idNivel=null)
    {
        $this->layout = 'ajax';
        $paralelos = $this->Paralelo->find('all', array(
            'recursive'=>0,
            'conditions'=>array(
                'nivele_id'=>$idNivel
            )
        ));
        $this->set(compact('paralelos', 'idNivel'));    
        //''debug($paralelos);
    }

    function insertar()
    {
        $gestiones = $this->Gestione->find('all', array('fields'=>array('Gestione.nombre', 'Gestione.nombre')));
        
        if (!empty($this->request->data))
        {
            
            if ($this->Nivele->save($this->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente!!!', 'msgbueno');
                $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        $this->set(compact('gestiones'));
    }

    function editar($id = null)
    {
        $this->Nivele->id = $id;
        if (!$id)
        {
            $this->Session->setFlash('No Existe el tipo de Nivele');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data))
        {
            $this->data = $this->Nivele->read();
             $gestiones = $this->Gestione->find('all', array('fields'=>array('Gestione.nombre', 'Gestione.nombre')));
             $this->set(compact('gestiones'));
        } else
        {
            if ($this->Nivele->save($this->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente el Nivel','msgbueno');
                $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash('Error al guardar el Nivele', 'msgerror');
            }
        }
    }

    function eliminar($id = null)
    {
        $this->Nivele->id = $id;
        $this->data = $this->Nivele->read();
        if (!$id)
        {
            $this->Session->setFlash('No existe el Nivel a Eliminar');
            $this->redirect(array('action' => 'index'));
        } else
        {
            if ($this->Nivele->delete($id))
            {
                $this->Session->setFlash('Se elimino el Nivel ' . $this->data['Nivele']['nombre']);
                $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
    
    public function asignamaterias($idNivel=null)
    {
        $nivel = $this->Nivele->find('first', array(
            'recursive'=>-1,
            'conditions'=>array(
                'Nivele.id'=>$idNivel
            )
        ));   
        $idNivel = $nivel['Nivele']['id']; 
        
        $materias = $this->Materia->find('all', array(
            'recursive'=>-1,
            'fields'=>array('id', 'nombre')                       
        ));
        
        if(!empty($this->request->data))
        {            
            $datos = $this->request->data;
            //debug($datos);
            //exit;
            $gestion=date("Y");
            $this->request->data="";
            foreach($datos['materias'] as $d)
            {
                $this->MateriasNivele->create();
                $this->request->data['MateriasNivele']['nivele_id']=$idNivel;
                $this->request->data['MateriasNivele']['materia_id']=$d;
                $this->request->data['MateriasNivele']['gestion']=$gestion;    
                $this->MateriasNivele->save($this->request->data);                
            }
            $this->Session->setFlash('Materias guardadas!!!', 'msgbueno');   
            $this->redirect(array('controller'=>'MateriasNiveles', 'action'=>'listamaterias', $idNivel));
            //debug($this->request->data);
            //exit;
        }
        
        $this->set(compact('materias', 'nivel'));
    }
    public function ajaxnuevoparalelonivel($idNivel = null)
    {
        $this->layout = 'ajax';
    
     if(!empty($this->request->data))
        {
            $nivel = $this->Nivele->find('first', array(
             'conditions'=>array('Nivele.id'=>$idNivel), 
             'recursive'=>-1
             ));
            $this->request->data['Paralelo']['nivele_id'] = $idNivel;
            $this->request->data['Paralelo']['curso'] = $nivel['Nivele']['nombre'];
           
           $paralelo = $this->request->data['Paralelo']['nombre'];
           $curso = $nivel['Nivele']['nombre'];
            if($this->Paralelo->save($this->request->data))
            {
                $this->Session->setFlash("Se inserto el paralelo $paralelo al curso $curso!!!", 'msgbueno');
                $this->redirect(array('controller'=>'Niveles', 'action' => 'index', $idNivel));
            }
        }    
        $gestiones = $this->Gestione->find('all', array('fields'=>array('Gestione.nombre', 'Gestione.nombre')));
        $this->set(compact('idNivel', 'nivel', 'gestiones'));
    }
    
}
